package com.springboot.framework.service.impl;

import com.springboot.framework.dao.entity.UserAddress;
import com.springboot.framework.dao.mapper.UserAddressMapper;
import com.springboot.framework.enums.StatusEnum;
import com.springboot.framework.exception.BusinessException;
import com.springboot.framework.model.UserAddressModel;
import com.springboot.framework.service.BuyerAddressService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author SWF
 * @Date 2019/6/10 23:03
 **/
@Service
public class BuyerAddressServiceImpl implements BuyerAddressService {
    @Resource
    private UserAddressMapper userAddressMapper;


    @Override
    public UserAddressModel get(Integer addressId) {
        UserAddress userAddress = userAddressMapper.selectByPrimaryKey(addressId);
        return convertUserAddressModel(userAddress);
    }

    @Override
    public UserAddressModel get(String openId, String address) {
        UserAddress userAddress = new UserAddress();
        userAddress.setUserWechatOpenid(openId);
        userAddress.setAddress(address);
        userAddress.setStatus(StatusEnum.NORMAL.getCode());
        userAddress = userAddressMapper.selectOne(userAddress);
        return convertUserAddressModel(userAddress);
    }

    @Override
    public List<UserAddressModel> list(String openId) {
        Example example = new Example(UserAddress.class);
        example.and().andEqualTo("userWechatOpenid" , openId)
            .andNotEqualTo("status", StatusEnum.DELETE.getCode());
        List<UserAddress> userAddresses = userAddressMapper.selectByExample(example);
        return convertUserAddressModel(userAddresses);
    }

    @Override
    public UserAddressModel insert(UserAddressModel userAddressModel) {
        UserAddress userAddress = convertUserAddress(userAddressModel);
        userAddressMapper.insertSelective(userAddress);
        return convertUserAddressModel(userAddress);
    }

    @Override
    public UserAddressModel update(UserAddressModel userAddressModel) {
        UserAddress userAddress = convertUserAddress(userAddressModel);
        userAddressMapper.updateByPrimaryKeySelective(userAddress);
        return userAddressModel;
    }

    @Override
    public void delete(UserAddressModel userAddressModel) {
        UserAddress userAddress = new UserAddress();
        userAddress.setId(userAddressModel.getId());
        userAddress.setStatus(StatusEnum.DELETE.getCode());
        int resultCount = userAddressMapper.updateByPrimaryKeySelective(userAddress);
        if(resultCount == 0){
            throw new BusinessException("地址删除失败");
        }
    }

    private UserAddressModel convertUserAddressModel(UserAddress userAddress){
        if(userAddress == null){
            return null;
        }
        UserAddressModel userAddressModel = new UserAddressModel();
        BeanUtils.copyProperties(userAddress,userAddressModel);
        return userAddressModel;
    }

    private List<UserAddressModel> convertUserAddressModel(List<UserAddress> userAddresses){
        if(userAddresses == null){
            return null;
        }
        List<UserAddressModel> userAddressModels = userAddresses.stream()
                .map(e->convertUserAddressModel(e))
                .collect(Collectors.toList());
        return userAddressModels;
    }

    private UserAddress convertUserAddress(UserAddressModel userAddressModel){
        UserAddress userAddress = new UserAddress();
        BeanUtils.copyProperties(userAddressModel,userAddress);
        return userAddress;
    }
}
